package nc.tb.app.plantable.ui;

import java.awt.BorderLayout;
import java.util.ArrayList;
import java.util.HashMap;

import nc.tb.app.CommonPlan;
import nc.tb.app.plantable.format.INtbPlanTableFmt;
import nc.tb.app.plantable.format.NtbPlanTableFmtGenarator;
import nc.tb.cube.CubeView;
import nc.tb.cube.DimLayout;
import nc.tb.cube.DimVector;
import nc.tb.cube.SheetDef;
import nc.tb.cube.cell.DataCell;
import nc.tb.cube.model.Plan;
import nc.tb.dim.DimDef;
import nc.tb.dim.DimMember;
import nc.tb.report.plugin.ICubeViewPanel;
import nc.tb.report.plugin.TopDimPanel;
import nc.tb.ufo.ui.IMode;
import nc.tb.ufo.ui.TitleDimMemberSelectionChangeListener;
import nc.ui.pub.beans.UIPanel;

public class NtbPlanTablePanel extends UIPanel implements 
		ICubeViewPanel, TitleDimMemberSelectionChangeListener {

	private UIPanel uiCenterPanel = null;
	private TopDimPanel topDimPanel = null;
	private NtbPlanEditCtlPanel ntbPlanEditCtlPanel = null;
	private NtbPlanEditTable ntbPlanEditTable = null;
	
	private Plan m_plan = null;
	private CubeView m_cubeView = null;
	private int nMode = IMode.PLAN;
	private DimLayout m_dimLayout = null;
	private INtbPlanTableFmt tableFmt = null;

	/**
	 * This method initializes 
	 * 
	 */
	public NtbPlanTablePanel(Plan plan, CubeView cbView, int nMode) {
		super();
		m_plan = plan;
		m_cubeView = cbView;
		this.nMode = nMode;
		initialize();
//		initDimPanel();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setLayout(new BorderLayout());
        this.add(getUiCenterPanel(), BorderLayout.CENTER);
        this.add(getTopDimPanel(), BorderLayout.NORTH);
	}

	/**
	 * This method initializes uiCenterPanel	
	 * 	
	 * @return nc.ui.pub.beans.UIPanel	
	 */
	private UIPanel getUiCenterPanel() {
		if (uiCenterPanel == null) {
			uiCenterPanel = new UIPanel();
			uiCenterPanel.setLayout(new BorderLayout());
			uiCenterPanel.add(getNtbPlanEditTable(), BorderLayout.CENTER);
			uiCenterPanel.add(getNtbPlanEditCtlPanel(), BorderLayout.NORTH);
		}
		return uiCenterPanel;
	}

	/**
	 * This method initializes topDimPanel	
	 * 	
	 * @return nc.tb.report.plugin.TopDimPanel	
	 */
	private TopDimPanel getTopDimPanel() {
		if (topDimPanel == null) {
			topDimPanel = new TopDimPanel(this);
			topDimPanel.addTitleDimMemberSelectionChangeListener(this);
			initDimPanel();
		}
		return topDimPanel;
	}

	/**
	 * This method initializes ntbPlanEditCtlPanel	
	 * 	
	 * @return nc.tb.app.plantable.ui.NtbPlanEditCtlPanel	
	 */
	private NtbPlanEditCtlPanel getNtbPlanEditCtlPanel() {
		if (ntbPlanEditCtlPanel == null) {
			ntbPlanEditCtlPanel = new NtbPlanEditCtlPanel(getNtbPlanEditTable());
			ntbPlanEditCtlPanel.setButtonEnable(m_plan.isCheckedOut());
		}
		return ntbPlanEditCtlPanel;
	}

	/**
	 * This method initializes ntbPlanEditTable	
	 * 	
	 * @return nc.tb.app.plantable.ui.NtbPlanEditTable	
	 */
	protected NtbPlanEditTable getNtbPlanEditTable() {
		if (ntbPlanEditTable == null) {
			ntbPlanEditTable = new NtbPlanEditTable(this/*m_plan, m_cubeView*/);
//			ntbPlanEditTable.setPreferredSize(new Dimension(620, 483));
		}
		return ntbPlanEditTable;
	}
	
	public SheetDef getSheetDef() {
		return m_plan == null ? null : m_plan.getSheetDef();
	}
	
	public void titleDimMemberSelectionChanged(DimDef dd, DimMember dm) {
		DimLayout dl = getDimLayout();//m_cubeView.getDimLayout();
		if (dl != null) {
			DimVector dv = dl.getTitleDimVector();
			if (dv != null) {
				dv.setDimMember(dm);
				fireTitleDimChanged();
			}
		}
	}

	public void titleDimMembersSelectionChanged(DimDef[] dd, DimMember[] dm) {
		
	}
	
	private void fireTitleDimChanged() {
		getNtbPlanEditTable().fireTitleDimChanged();
	}
	
	private void initDimPanel() {
		HashMap<DimDef, DimMember> hmDD2DM = new HashMap<DimDef, DimMember>();
		HashMap<DimDef, ArrayList<DimMember>> hmDD2AllDM = new HashMap<DimDef, ArrayList<DimMember>>();

		DimLayout dl = getDimLayout();
		if(dl == null || dl.getTitleDimDefs() == null || dl.getTitleDimDefs().length == 0) {
			getTopDimPanel().setVisible(false);
			return;
		}
		ArrayList<DataCell> cells = m_plan.getDataCellsList();
		DimDef[] dds = dl.getTitleDimDefs();
		if (cells != null && cells.size() > 0) {
			for (int i=0 ; i<cells.size() ; i++) {
				DimVector dv = cells.get(i).getDimVector();
				for(int j=0 ; j<dds.length ; j++){
					if(!hmDD2AllDM.containsKey(dds[j])){
						hmDD2AllDM.put(dds[j], new ArrayList<DimMember>());
					}
					ArrayList<DimMember> dmlist = hmDD2AllDM.get(dds[j]);
					if(!dmlist.contains(dv.getDimMemberByDimDef(dds[j]))){
						dmlist.add(dv.getDimMemberByDimDef(dds[j]));
					}
				}
			}
			
			for(int i=0 ; i<dds.length ; i++){
				DimVector top = dl.getTitleDimVector();
				if(top != null){
					DimMember topdm = top.getDimMemberByDimDef(dds[i]);
					if(hmDD2AllDM.containsKey(dds[i]) && hmDD2AllDM.get(dds[i]).contains(topdm)){
						hmDD2DM.put(dds[i], topdm);
					}else if(hmDD2AllDM.containsKey(dds[i]) && !hmDD2AllDM.get(dds[i]).contains(topdm)){
						hmDD2DM.put(dds[i], hmDD2AllDM.get(dds[i]).get(0));
					}
				}
				else{
					getTopDimPanel().setVisible(false);
					return;
				}
			}
			getTopDimPanel().setVisible(true);
			getTopDimPanel().setData(hmDD2DM, hmDD2AllDM, getVisibleRect().getWidth());
		}
	}

	public CubeView getCubeView() {
		return m_cubeView;
	}

	public Plan getPlan() {
		return m_plan;
	}

	public int getMode() {
		return nMode;
	}
	public void setMode(int mode) {
		nMode = mode;
	}
	
	protected DimLayout getDimLayout() {
		if (m_dimLayout == null && m_cubeView != null) {
			m_dimLayout = m_cubeView.getDimLayout();
			if (m_dimLayout == null) {
				m_dimLayout = DimLayout.autoCreateDimLayout(m_plan, m_cubeView);
				if (m_plan instanceof CommonPlan && m_dimLayout.getTitleDimVector() == null) {
					m_dimLayout.setTitleDimVector(((CommonPlan)m_plan).getParaDimVector());
				}
			}
		}
		return m_dimLayout;
	}
	
	protected INtbPlanTableFmt getPlanTableFmt() {
		if (tableFmt == null) {
			HashMap args = new HashMap();
			args.put(NtbPlanTableFmtGenarator.Key_plan, m_plan);
			tableFmt = NtbPlanTableFmtGenarator.getNtbPlanTableFmt(args);
		}
		return tableFmt;
	}
	
	protected void setEditable(boolean b) {
		getNtbPlanEditCtlPanel().setButtonEnable(b);
		getNtbPlanEditTable().setEditable(b);
	}

}
